package cn.tedu.vrd07.controller;

import cn.tedu.vrd07.entity.Product;
import cn.tedu.vrd07.entity.User;
import cn.tedu.vrd07.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("insert")
    public int insert(Product product, MultipartFile picFile,
                      HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        System.out.println("product = " + product + ", picFile = " + picFile);
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //得到和日期相关的路径
        SimpleDateFormat format = new SimpleDateFormat("/yyyy/MM/dd/");
        //把当前时间按照指定的格式进行转换
        String datePath = format.format(new Date());
        //得到保存图片文件夹的路径
        File dirFile = new File(dirPath+datePath);
         if (!dirFile.exists()){
             dirFile.mkdirs();
         }
         //得到完整的文件路径
         String filePath = dirPath+datePath+fileName;
         //保存上传的文件
        picFile.transferTo(new File(filePath));
        //图片路径添加到product对象里面
        product.setUrl(datePath+fileName);
        product.setCreated(new Date());

        mapper.insert(product);
        return 1;
    }
    @RequestMapping("select")
    public List<Product> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(int id){
        //通过id查url
        String url = mapper.selectById(id);
        String filePath = dirPath+url;
        new File(filePath).delete();
        mapper.delete(id);
        mapper.delete(id);
    }
    @RequestMapping("selectforindex")
    public List<Product> selectForIndex(){
        return mapper.selectForIndex();
    }
    @RequestMapping("selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }
    @RequestMapping("selectByKw")
    public List<Product> selectByKw(String kw){
        return mapper.selectByKw(kw);
    }
    @RequestMapping("selectView")
    public List<Product> selectView(){
        return mapper.selectView();
    }
    @RequestMapping("selectLike")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }
    @RequestMapping("detail")
    public Product detail(int id,HttpSession session){
        String info = (String) session.getAttribute("view"+id);
        if (info == null){
            //浏览量加一
            mapper.viewById(id);
            session.setAttribute("view"+id,"浏览过了");
        }

        return mapper.detail(id);
    }
    @RequestMapping("like")
    public int like(int id,HttpSession session){
        //从会话对象中取出点赞时保存的数据
        String info = (String) session.getAttribute("like"+id);
        //如果得到的是null说明没有点过赞
        if (info == null) {
            //修改数据库
            mapper.likeById(id);
            //把点过赞的作品id保存到会话对象
            session.setAttribute("like" + id, "点过赞了!");
            return 1;
        }
        return 2;
    }
}
